package com.shimao.celnet.payment.controller;

import com.shimao.celnet.payment.model.PPay;
import com.shimao.celnet.payment.service.PPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "payment")
public class PaymentController {
    @Autowired
    private PPayService pPayService;

    @RequestMapping(value ="paytwo",method = {RequestMethod.GET})
    public String getPaymentType(Model model, HttpServletRequest req, HttpServletResponse res){
        String paymentId = req.getParameter("paymentID");
        System.out.println("====paymentId===="+paymentId);
        //如果get方式传进来的参数为空，跳转到支付失败
        if(paymentId == null || paymentId==""){
            return "payment/paymentFail";
        }
        if(paymentId!=null && paymentId != ""){
            PPay pPay = pPayService.getByKey(paymentId);
            //如果没有找到该笔订单的数据，跳转到支付失败页面
            if(pPay!=null) {
                String totalMoney = pPay.getPayAmount();
                model.addAttribute("totalMoney", Float.valueOf(totalMoney)/100.0);
            }else{
                return "payment/paymentFail";
            }
        }
        return "payment/payment";
    }

    @RequestMapping(value ="paySuccess",method = RequestMethod.GET)
    public String getSuccess(){
        return "payment/paymentSuccess";
    }

    @RequestMapping(value ="payError",method = RequestMethod.GET)
    public String getError(){
        return "payment/paymentFail";
    }

    @RequestMapping(value ="payWait",method = RequestMethod.GET)
    public String getWait(){
        return "payment/paymentWait";
    }

}
